<template>
    <div>
        <!-- 搜索表单 -->
        <el-form :inline="true" :model="searchFrom" ref="searchFrom" class="demo-form-inline" style="text-align:center">
            <el-form-item label="名称" prop="carName">
                <el-input v-model="searchFrom.carName" placeholder="名称"></el-input>
            </el-form-item>
            <el-form-item label="车牌号" prop="vin">
                <el-input v-model="searchFrom.vin" placeholder="车牌号"></el-input>
            </el-form-item>
            <el-form-item label="门店">
                <el-select v-model="searchFrom.shopId" filterable clearable placeholder="选择门店" style="width: 130px">
                    <el-option
                            v-for="item in shop"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="searchBtn()">查询</el-button>
                <el-button type="warning" icon="el-icon-refresh" @click="resetForm()">重置</el-button>
            </el-form-item>
        </el-form>

        <!-- 表格 -->
        <el-table :data="tableData" stripe style="width: 100%">
            <el-table-column prop="id" label="编号"/>
            <el-table-column prop="carName" label="车名"/>
            <el-table-column prop="coverImg" label="图片">
                <!--插入图片链接的代码-->
                <template slot-scope="scope">
                    <img  :src="scope.row.coverImg" alt="" style="width: 100px;height: 100px">
                </template>
            </el-table-column>
            <el-table-column prop="brandName" label="品牌"/>
            <el-table-column prop="typeName" label="类型"/>
            <el-table-column prop="vin" label="车牌号"/>
            <el-table-column prop="buyPrice" label="购买价格"/>
            <el-table-column prop="price" label="出租价格"/>
            <el-table-column label="状态">
                <template slot-scope="scope">
                    <span v-if="scope.row.state === 1"><el-button size="mini" type="success">可租</el-button></span>
                    <span v-if="scope.row.state === 2"><el-button size="mini" type="warning">未归还</el-button></span>
                    <span v-if="scope.row.state === 3"><el-button size="mini" type="primary">维修中</el-button></span>
                    <span v-if="scope.row.state === 4"><el-button size="mini" type="danger">报废</el-button></span>
                </template>
            </el-table-column>
            <el-table-column prop="shopName" label="门店"/>
            <el-table-column prop="carConfig.name" label="配置名称"/>
            <el-table-column prop="carConfig.displacement" label="排量(L)"/>
            <el-table-column prop="carConfig.fcphk" label="百公里油耗(L)"/>
            <el-table-column prop="carConfig.seating" label="座位数(个)"/>
            <el-table-column prop="intro" label="介绍"/>
            <el-table-column prop="addtime" label="添加时间"/>
            <el-table-column align="right" width="200">
                <template slot="header">
                    <span style="margin-right:30px">操作</span>
                    <el-button size="mini" type="primary" @click="openAddDialog()">添加</el-button>
                </template>
                <template slot-scope="scope">
                    <el-button-group>
                        <el-button v-if="scope.row.state !== 4" size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑
                        </el-button>
                        <el-button v-if="scope.row.state === 1" size="mini" type="primary"
                                   @click="handleService(scope.$index, scope.row)">维修
                        </el-button>
                        <el-button v-if="scope.row.state === 3" size="mini" type="primary"
                                   @click="handleReturn(scope.$index, scope.row)">返厂
                        </el-button>
                        <el-button v-if="scope.row.state === 1" size="mini" type="primary" @click="handleDelete(scope.$index,scope.row)">报废
                        </el-button>
                    </el-button-group>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="block" style="text-align:center">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-sizes="[1, 2, 5, 10]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

        <!-- 添加弹出框 -->
        <el-dialog title="添加车辆信息" :visible.sync="dialogAdd" width="700px">
            <el-form :model="car" ref="car" :rules="rules">
                <el-input v-model="car.id" style="display: none"></el-input>
                <el-form-item label="车名" label-width="120px" prop="carName">
                    <el-input v-model="car.carName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label-width="120px" label="汽车图片" prop="coverImg">
                    <div>
                        <el-upload
                                class="avatar-uploader"
                                action="http://localhost:12188/ss/user/upload"
                                :show-file-list="false"
                                :headers="headers"
                                :on-success="handleAvatarSuccess"
                                :before-upload="beforeAvatarUpload">
                            <img v-if="car.coverImg" :src="car.coverImg" width="100" height="100" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </div>
                </el-form-item>
                <!-- 下拉框 -->
                <el-form-item label="品牌" prop="brandId" label-width="120px">
                    <el-select v-model="car.brandId" placeholder="请选择品牌" style="width: 200px; margin-left: 20px">
                        <el-option :value="brand.id" :key="brand.id" :label="brand.name"
                                   v-for="brand in carBrands"></el-option>
                    </el-select>
                </el-form-item>
                <!-- 下拉框 -->
                <el-form-item label="门店" prop="shopId" label-width="120px">
                    <el-select v-model="car.shopId" placeholder="请选择门店" style="width: 200px; margin-left: 20px">
                        <el-option :value="item.id" :key="item.id" :label="item.name" v-for="item in shop"></el-option>
                    </el-select>
                </el-form-item>
                <!-- 下拉框 -->
                <el-form-item label="车牌类型" prop="type" label-width="120px">
                    <el-select v-model="car.type" placeholder="请选择车牌类型" style="width: 200px; margin-left: 20px">
                        <el-option :value="carType.id" :key="carType.id" :label="carType.typeName"
                                   v-for="carType in carTypes"></el-option>
                    </el-select>
                </el-form-item>
                <!-- 下拉框 -->
                <el-form-item label="汽车配置" prop="config" label-width="120px">
                    <el-select v-model="car.configId" placeholder="请选择汽车配置" style="width: 200px; margin-left: 20px">
                        <el-option :value="config.id" :key="config.id" :label="config.name"
                                   v-for="config in carConfig"></el-option>
                    </el-select>
                </el-form-item>
<!--                <el-form-item label="汽车状态">-->
<!--                    <el-radio-group v-model="car.state" size="medium">-->
<!--                        <el-radio border :label="1">可租</el-radio>-->
<!--                        <el-radio border :label="2">未归还</el-radio>-->
<!--                        <el-radio border :label="3">维修中</el-radio>-->
<!--                        <el-radio border :label="4">报废</el-radio>-->
<!--                    </el-radio-group>-->
<!--                </el-form-item>-->
                <el-form-item label="车牌号码" label-width="120px" prop="vin">
                    <el-input v-model="car.vin" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="进价" label-width="120px" prop="buyPrice">
                    <el-input-number v-model="car.buyPrice" :min="1" autocomplete="off"></el-input-number>
                </el-form-item>
                <el-form-item label="租价" label-width="120px" prop="price">
                    <el-input-number v-model="car.price" :min="1" autocomplete="off"></el-input-number>
                </el-form-item>
                <el-form-item label="介绍" label-width="120px" prop="price">
                    <el-input type="textarea" v-model="car.intro" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="添加时间" label-width="120px" prop="addtime">
                    <el-col :span="11">
                        <el-form-item prop="addtime">
                            <el-date-picker type="date"
                                            format="yyyy-MM-dd"
                                            value-format="yyyy-MM-dd"
                                            placeholder="选择日期" v-model="car.addtime"
                                            style="width: 100%;"></el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="resetForm()">取 消</el-button>
                <el-button type="primary" @click="addShopBtn()">确 定</el-button>
            </div>
        </el-dialog>

        <!-- 维修弹出框 -->
        <el-dialog title="车辆维修记录信息" :visible.sync="dialogReturn">
            <el-form :model="maintain" ref="maintain" :rules="returnRule">
                <el-input v-model="maintain.id" style="display: none"/>
                <el-form-item label="车牌号" label-width="120px" prop="carId">
                    <el-input v-model="maintain.carId" disabled></el-input>
                </el-form-item>
                <!-- 下拉框 -->
                <el-form-item label="维修厂" prop="maintainShopId" label-width="120px">
                    <el-select v-model="maintain.maintainShopId" placeholder="请选择维修厂"
                               style="width: 200px; margin-left: 20px">
                        <el-option :value="item.id" :key="item.id" :label="item.name"
                                   v-for="item in maintainShop"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="金额" label-width="120px" prop="amount">
                    <el-input-number v-model="maintain.amount" :min="1" autocomplete="off"></el-input-number>
                </el-form-item>
                <el-form-item label="添加时间" label-width="120px" prop="addtime">
                    <el-col :span="11">
                        <el-form-item prop="addtime">
                            <el-date-picker type="date"
                                            format="yyyy-MM-dd"
                                            value-format="yyyy-MM-dd"
                                            placeholder="选择日期" v-model="maintain.addtime"
                                            style="width: 100%;"></el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-form-item>
                <el-form-item label="备注" label-width="120px" prop="remark">
                    <el-input v-model="maintain.remark" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogReturn=false">取 消</el-button>
                <el-button type="primary" @click="maintainBtn()">确 定</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
    export default {
        data: function () {
            // 自定义验证规则 验证添加和修改时的品牌名字是否可用
            const checkVin = (rule, value, callback) => {
                this.$http.get("/cs/car/checkVin?vin=" + value + "&id=" + this.car.id)
                    .then((response) => {
                        if (response.data.resultCode === 5008) {
                            callback(new Error('该车牌号已存在！'));
                        } else {
                            callback();
                        }
                    })
            };
            return {
                tableData: [],
                headers: {
                    myAuthorization: window.localStorage.getItem("token"),
                },
                total: 0, // 总条数
                pageNum: 1, // 当前页数
                pageSize: 5, // 每页显示几条
                dialogAdd: false,
                dialogReturn: false,
                searchFrom: {},
                car: {},
                maintain: {},// 维修表单
                maintainShop: {},
                shop: [],  // 所有门店信息
                carBrands: [], // 所有的品牌信息
                carTypes: [], // 所有得的类型信息
                carConfig: [], // 所有的配置信息
                // 添加表单的规则
                rules: {
                    carName: [
                        {required: true, message: '请输入车名', trigger: 'blur'},
                    ],
                    brandId: [
                        {required: true, message: '请至少选择一个车牌', trigger: 'change'}
                    ],
                    type: [
                        {required: true, message: '请至少选择一个类型', trigger: 'change'}
                    ],
                    vin: [
                        {required: true, message: '请输入车牌号', trigger: 'blur'},
                        {
                            pattern: /^[京津晋冀蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新][ABCDEFGHJKLMNPQRSTUVWXY][\dABCDEFGHJKLNMxPQRSTUVWXYZ]{5}$/,
                            message: '车牌号格式错误', trigger: 'blur'
                        },
                        {trigger: 'blur', validator: checkVin}//自定义验证:调方法
                    ],
                    configId: [
                        {required: true, message: '请至少选择一个配置', trigger: 'change'}
                    ],
                    buyPrice: [
                        {required: true, message: '请输入进价', trigger: 'blur'},
                        {type: 'number', message: '进价必须为数字值', trigger: 'blur'},
                    ],
                    price: [
                        {required: true, message: '请输入租价', trigger: 'blur'},
                        {type: 'number', message: '租价必须为数字值', trigger: 'blur'},
                    ],
                    addtime: [
                        {required: true, message: '请输入时间', trigger: 'blur'},
                    ],
                    intro: [
                        {required: true, message: '请输入介绍', trigger: 'blur'},
                    ]
                },
                // 维修表单的规则
                returnRule: {
                    maintainShopId: [
                        {required: true, message: '请选择维修厂', trigger: 'blur'}
                    ],
                    amount: [
                        {required: true, message: '请输入金额', trigger: 'blur'},
                    ],
                    addtime: [
                        {required: true, message: '请输入时间', trigger: 'blur'},
                    ],
                    remark: [
                        {required: true, message: '请输入备注', trigger: 'blur'},
                    ],
                }
            }
        },
        methods: {
            // 重置表格
            resetForm() {
                this.searchFrom = {}
                this.car = {}
                this.dialogAdd = false
                this.getAllCar();
            },
            // 添加按钮
            openAddDialog() {
                this.car = {state: 1}
                this.dialogAdd = true;
            },
            // 打开编辑操作
            handleEdit(index, row) {
                this.dialogAdd = true // 打开弹出层
                // 让表单重新赋值
                this.car = JSON.parse(JSON.stringify(row))

            },
            // 送去维修按钮
            handleService(index, row) {
                // 执行修改操作 将车辆的状态改成 维修中
                this.$http.put("cs/car/update", {id: row.id, state: 3}).then(res => {
                    if (Number(res.data.resultCode) === 2000) {
                        this.$message({type: 'success', message: '车辆已送去维修'});
                        this.getAllCar();
                    } else {
                        this.$message({type: 'error', message: '系统异常'});
                    }
                })
            },
            // 返厂按钮
            handleReturn(index, row) {
                this.maintain = {carId: row.vin,id:row.id}
                // 执行修改操作 将车辆的状态改成 维修中
                this.dialogReturn = true
            },

            //每页显示几条改变
            handleSizeChange(val) {
                this.pageSize = val;
                this.getAllCar();
            },
            // 当前是第几页改变
            handleCurrentChange(val) {
                this.pageNum = val;
                this.getAllCar();
            },
            // 查询表单提交
            searchBtn() {
                this.pageNum = 1;
                console.log(this.searchFrom);
                this.getAllCar();
            },
            // 获得表格数据
            getAllCar() {
                this.$http.post('cs/car/queryAll?pageSize=' + this.pageSize + '&pageNum=' + this.pageNum, this.searchFrom)
                    .then(res => {
                        this.tableData = res.data.t.list;
                        this.total = res.data.t.total
                    })
            },
            // 添加\编辑表单提交
            addShopBtn() {
                this.$refs["car"].validate((valid) => {
                    if (valid) {
                        if (this.car.id === undefined) {
                            // 执行添加操作
                            this.$http.post("cs/car/insert", this.car).then(res => {
                                console.log(res.data.t);
                                if (Number(res.data.resultCode) === 2000) {
                                    this.$message({type: 'success', message: '恭喜你，添加成功'});
                                    this.dialogAdd = false;
                                    this.getAllCar();
                                } else {
                                    this.$message({type: 'error', message: '系统异常'});
                                }
                            })
                        } else {
                            // 执行修改操作
                            this.$http.put("cs/car/update", this.car).then(res => {
                                if (Number(res.data.resultCode) === 2000) {
                                    this.$message({type: 'success', message: '恭喜你，修改成功'});
                                    this.dialogAdd = false;
                                    this.getAllCar();
                                } else {
                                    this.$message({type: 'error', message: '系统异常'});
                                }
                            })

                        }

                    }
                })
            },
            // 返厂表单提交
            maintainBtn() {
                this.$refs["maintain"].validate((valid) => {
                    if (valid) {
                        this.$http.post('cs/car/carReturnMaintain', this.maintain)
                            .then(res => {
                                if (Number(res.data.resultCode) === 2000) {
                                    this.$message({type: 'success', message: '返厂成功'});
                                    this.dialogReturn = false;
                                    this.getAllCar();
                                } else {
                                    this.$message({type: 'error', message: '系统异常'});
                                }
                            })
                    }
                })

            },
            // 删除
            handleDelete(index, row) {
                this.$confirm('此操作将永久报废该汽车, 是否继续?', '提示', {
                    confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning', center: true
                }).then(() => {
                    this.$http.put("cs/car/update",{id:row.id,state:4}).then(res => {
                        if (Number(res.data.resultCode) === 2000) {
                            this.$message({type: 'success', message: '该汽车已报废!'});
                            this.getAllCar();
                        } else {
                            this.$message({type: 'error', message: '系统异常'});
                        }
                    })
                }).catch(() => {
                    this.$message({type: 'info', message: '已取消报废'});
                });
            },
            // 上传图片之前
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isGIF = file.type === 'image/gif';
                const isPNG = file.type === 'image/png';
                const isBMP = file.type === 'image/bmp';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG && !isGIF && !isPNG && !isBMP) {
                    this.common.errorTip('上传图片必须是JPG/GIF/PNG/BMP 格式!');
                }
                if (!isLt2M) {
                    this.common.errorTip('上传图片大小不能超过 2MB!');
                }
                return (isJPG || isBMP || isGIF || isPNG) && isLt2M;
            },
            // 品牌logo上传成功回调
            handleAvatarSuccess(res, file) {
                this.car.coverImg = res.t;
            },

        },
        mounted() {
            this.getAllCar();
            // 获取所有的门店
            this.$http.post('cs/shop/queryAll?pageSize=1000000000&pageNum=1', {})
                .then(res => {
                    this.shop = res.data.t.list;
                })
            // 获取所有的品牌信息
            this.$http.post('cs/brand/queryAll?pageSize=' + 10000 + '&pageNum=' + 1, {})
                .then(res => {
                    this.carBrands = res.data.t.list;
                })
            // 获取所有的类型信息
            this.$http.post('cs/carType/queryAll?pageSize=' + 100000 + '&pageNum=' + 1, {})
                .then(res => {
                    this.carTypes = res.data.t.list;
                })
            // 获取所有的配置信息
            this.$http.post('cs/carConfig/queryAll?pageSize=' + 10000 + '&pageNum=' + 1, this.searchFrom)
                .then(res => {
                    this.carConfig = res.data.t.list;
                })
            // 获取所有的维修门店
            this.$http.post('cs/maintainShop/queryAll?pageSize=1000000000&pageNo=1', {})
                .then(res => {
                    this.maintainShop = res.data.t.list;
                })
        }
    }
</script>

<style>

</style>